import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:jubobao/config/JuBoTabBarConfig.dart';
import 'package:jubobao/other/Login/JuBoLogin.dart';
import 'package:jubobao/other/Tool/JuBoToolManager.dart';
import 'package:jubobao/other/routeManager/JuBoRouteManager.dart';
import 'package:jubobao/profile/approval/JuBoApproval.dart';
import 'package:jubobao/profile/attention/JuBoAttention.dart';
import 'package:jubobao/provide/JuBoLoginStateManager.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
import 'i18n.dart';
import 'package:flutter/services.dart';
import 'package:orientation/orientation.dart';

void main() {


  var loginStateManager = JuBoLoginState();
  var provides = Providers();
  provides..provide(Provider<JuBoLoginState>.value(loginStateManager));
  runApp(
      ProviderNode(child: MyApp(),providers: provides,)
  );

  if (Platform.isAndroid) {
    //沉浸式状态栏
    //写在组件渲染之后，是为了在渲染后进行设置赋值，覆盖状态栏，写在渲染之前对MaterialApp组件会覆盖这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);


}

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        backgroundColor:Colors.red,
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
          primaryColor: Color(0xff1594FA)
      ),
      home: MyHomePage(title: '首页'),
      routes: JuBoRoutes.routeMap,
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [
        Locale("zh", "CH"),
        Locale("en", "US"),
      ],
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage>  {

  int _index = 0;
  bool _isExits;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loginState();
  }

  loginState() async {
    _isExits = await JuBoToolManager.getLoginState();
  }

  @override
  Widget build(BuildContext context)
  {
    return Provide<JuBoLoginState>(
        builder: (context,child,login){
          loginState();
          return !login.loginState  ? JuBoLogin() : Scaffold(
            body: IndexedStack(
              children: pages,
              index: _index,
            ),
            bottomNavigationBar: BottomNavigationBar(
              items: tabbars,
              currentIndex: _index,
              type: BottomNavigationBarType.fixed,
              onTap: (index){
                setState(() {
                  _index = index;
                });
              },
            ),
            // This trailing comma makes auto-formatting nicer for build methods.
          );
    });
  }
}